package com.bookstore.backend.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bookstore.backend.exception.ServiceException;
import com.bookstore.backend.mapper.BookMapper;
import com.bookstore.backend.model.entity.Book;
import com.bookstore.backend.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: world
 * @date: 2022/7/29 15:43
 * @description: Book表的实现类
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {

    @Autowired
    private BookMapper bookMapper;

    /**
     * 查询列表
     * @return
     */
    @Override
    public List<Book> selectList(String isbn) {
        List<Book>result = bookMapper.selectList(isbn);
        if (result.isEmpty()){
            throw new ServiceException("书本不存在!");
        }
        return result;
    }
    @Override
    public Integer insert(Book book) {
        if (book.getBookName() == null){
            throw new ServiceException("书名不能为空");
        }
        if (book.getAuthor() == null){
            throw new ServiceException("图书作者不能为空");
        }
        if (book.getCategory() == null){
            throw new ServiceException("图书类别不能为空");
        }
        if (book.getPress() == null){
            throw new ServiceException("出版社不能为空");
        }
        if (book.getBookNum() == null){
            throw new ServiceException("图书总数不能为空");
        }
        if (book.getLibraryNum() == null){
            throw new ServiceException("在库总数不能为空");
        }
        if (book.getIsbn() == null){
            throw new ServiceException("图书isbn不能为空");
        }
        if (book.getPrice() == null){
            throw new ServiceException("图书价格不能为空");
        }
        return bookMapper.insert(book);
    }

    @Override
    public Integer updateNumBybid(Book book) {
        return bookMapper.updateNumBybid(book);
    }

    @Override
    public Integer deleteNumBybid(Integer bid) {
        Integer integer = bookMapper.deleteNumBybid(bid);
        if (integer == 0){
            throw new ServiceException("请选择图书");
        }
        return integer;
    }

    //分页
    public IPage<Book> selectBookPage(Page<Book> page, Integer state){
        return bookMapper.selectPageVO(page,state);
    }

}
